/**
 * 
 */
package org.log4jeditor.xml.editor.contentassist.template;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.wst.xml.ui.internal.contentassist.ReplaceNameTemplateContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.contentassist.template.BasePackageVariableResolver;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.WorkbenchTasks;

/**
 * @author Xi
 *
 */
@SuppressWarnings("restriction")
public class BasePackageVariableResolverTestCase extends AbstractUITestCase {

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-contentassist.xml");
	}
	
	@After
	public void teaDown() throws Exception {
		super.tearDownProject();
	}

	/**
	 * Test method for {@link org.log4jconfig.xml.editor.contentassist.template.BasePackageVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)}.
	 * @throws BadLocationException 
	 */
	@Test
	public void testResolveTemplateVariableTemplateContext() throws BadLocationException {
		int offset = sourceViewer.getDocument().getLineOffset(22) + 22;
		BasePackageVariableResolver resolver = new BasePackageVariableResolver();
		TemplateVariable variable = new TemplateVariable("", "pkg", "", new int[]{0});
		ReplaceNameTemplateContext context = new ReplaceNameTemplateContext(null, super.document, new Position(offset));
		resolver.resolve(variable, context);
		Assert.assertNotNull(variable.getValues());
		Assert.assertEquals("Wrong number of variable replacement proposals", 2, variable.getValues().length);
		Assert.assertEquals("Wrong variable replacement proposal value", "com", variable.getValues()[0]);
		//Assert.assertEquals("Expected 3 variable replacement proposal value", "com.example", variable.getValues()[1]);
		Assert.assertEquals("Wrong variable replacement proposal value", "test", variable.getValues()[1]);
	}
	
	/**
	 * Test method for {@link org.log4jconfig.xml.editor.contentassist.template.BasePackageVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)}.
	 * @throws BadLocationException 
	 */
	@Test
	public void testResolveTemplateVariableTemplateContextNone() throws BadLocationException {
		int offset = sourceViewer.getDocument().getLineOffset(22) + 22;
		BasePackageVariableResolver resolver = new BasePackageVariableResolver();
		TemplateVariable variable = new TemplateVariable("", "pkx", "", new int[]{0});
		ReplaceNameTemplateContext context = new ReplaceNameTemplateContext(null, super.document, new Position(offset));
		resolver.resolve(variable, context);
		Assert.assertNotNull(variable.getValues());
		Assert.assertEquals("Expected 1 variable replacement proposal", 1, variable.getValues().length);
		Assert.assertEquals("Expected empty variable replacement proposal value", "", variable.getValues()[0]);
	}
	
	/**
	 * Test method for {@link org.log4jconfig.xml.editor.contentassist.template.BasePackageVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)}.
	 * @throws BadLocationException 
	 * @throws CoreException 
	 */
	@Test
	public void testResolveTemplateVariableTemplateContextNoSourcePackage() throws BadLocationException, CoreException {
		//project.getFolder(new Path("src")).delete(true, new NullProgressMonitor());
		WorkbenchTasks.removeSourceFolder(javaProject.getProject(), new NullProgressMonitor());
		int offset = sourceViewer.getDocument().getLineOffset(22) + 22;
		BasePackageVariableResolver resolver = new BasePackageVariableResolver();
		TemplateVariable variable = new TemplateVariable("", "pkx", "", new int[]{0});
		ReplaceNameTemplateContext context = new ReplaceNameTemplateContext(null, super.document, new Position(offset));
		resolver.resolve(variable, context);
		Assert.assertEquals("Expected empty variable replacement proposal", 1, variable.getValues().length);
		Assert.assertEquals("Expected empty variable replacement proposal", "", variable.getValues()[0]);
	}

}
